package com.todo.biz.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.biz.application.backend.service.FeedbackBackendService;
import com.todo.biz.application.convert.BizBusinessConvert;
import com.todo.biz.application.entity.bo.FeedbackBO;
import com.todo.biz.application.entity.bo.FeedbackDetailBO;
import com.todo.biz.application.entity.bo.FeedbackReplyBO;
import com.todo.biz.application.entity.condition.FeedbackConditionBO;
import com.todo.biz.application.service.FeedbackService;
import com.todo.biz.iface.backend.request.AuditFeedbackReq;
import com.todo.biz.iface.backend.request.QueryFeedbackByConditionReq;
import com.todo.biz.iface.backend.request.QueryFeedbackByIdReq;
import com.todo.biz.iface.backend.response.QueryFeedbackByConditionResp;
import com.todo.biz.iface.backend.response.QueryFeedbackByIdResp;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (Feedback)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:46
 */
@Slf4j
@Service("feedbackBackendService")
public class FeedbackBackendServiceImpl implements FeedbackBackendService {

    @Resource
    private FeedbackService feedbackService;

    @Override
    public BizResponse<Boolean> auditFeedback(AuditFeedbackReq req) {
        if (req == null) {
            return BizResponse.operationFailed("参数为空");
        }
        FeedbackDetailBO feedbackDetailBO = feedbackService.queryFeedbackById(req.getId());
        if (feedbackDetailBO == null) {
            return BizResponse.operationFailed("反馈不存在");
        }
        FeedbackReplyBO feedbackReplyBO = new FeedbackReplyBO();
        feedbackReplyBO.setId(req.getId());
        feedbackReplyBO.setReplyUser(String.valueOf(req.get_staff().getUserId()));
        feedbackReplyBO.setReplyContent(req.getReplyContent());
        feedbackReplyBO.setReplyTime(new Date());
        Boolean updated = feedbackService.replyFeedback(feedbackReplyBO);
        if (updated) {
            log.error("FeedbackBackendService#auditFeedback 反馈审核失败 入参:{}", JSONObject.toJSONString(req));
            return BizResponse.success(true);
        }
        return BizResponse.operationFailed();
    }

    @Override
    public BizPageResponse<QueryFeedbackByConditionResp> queryFeedbackByCondition(QueryFeedbackByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            return BizPageResponse.operationFailed("参数为空");
        }
        FeedbackConditionBO feedbackCondition = BizBusinessConvert.convertToFeedbackConditionBO(req);
        PageResult<FeedbackBO> feedbackBOPageResult = feedbackService.queryFeedbackByCondition(feedbackCondition);

        return BizPageResponse.success(BizBusinessConvert.convertToQueryFeedbackByConditionRespPage(feedbackBOPageResult));
    }

    @Override
    public BizResponse<QueryFeedbackByIdResp> queryFeedbackById(QueryFeedbackByIdReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.operationFailed("参数为空");
        }
        FeedbackDetailBO feedbackDetailBO = feedbackService.queryFeedbackById(req.getId());
        return BizResponse.success(BizBusinessConvert.convertToQueryFeedbackByIdResp(feedbackDetailBO));
    }
}
